package com.shuda.business.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuda.business.crm.entity.FollowUpMethodEntity;
import com.shuda.business.crm.mapper.FollowUpMethodMapper;
import com.shuda.business.crm.service.FollowUpMethodService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 跟进方式配置Service实现类
 * @author shuda
 */
@Service
@RequiredArgsConstructor
public class FollowUpMethodServiceImpl extends ServiceImpl<FollowUpMethodMapper, FollowUpMethodEntity> 
        implements FollowUpMethodService {

    private final FollowUpMethodMapper followUpMethodMapper;

    @Override
    public IPage<FollowUpMethodEntity> getFollowUpMethodPage(Page<FollowUpMethodEntity> page, String methodName, String status) {
        LambdaQueryWrapper<FollowUpMethodEntity> queryWrapper = new LambdaQueryWrapper<>();
        
        if (methodName != null && !methodName.trim().isEmpty()) {
            queryWrapper.like(FollowUpMethodEntity::getMethodName, methodName);
        }
        
        if (status != null && !status.trim().isEmpty()) {
            queryWrapper.eq(FollowUpMethodEntity::getStatus, status);
        }
        
        queryWrapper.orderByAsc(FollowUpMethodEntity::getSortOrder)
                   .orderByDesc(FollowUpMethodEntity::getCreateTime);
        
        return this.page(page, queryWrapper);
    }

    @Override
    public FollowUpMethodEntity getFollowUpMethodById(Long id) {
        return this.getById(id);
    }

    @Override
    public FollowUpMethodEntity getFollowUpMethodByCode(String methodCode, Long tenantId) {
        return followUpMethodMapper.selectByMethodCode(methodCode, tenantId);
    }

    @Override
    public List<FollowUpMethodEntity> getEnabledFollowUpMethods() {
        return followUpMethodMapper.selectEnabledMethods();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean createFollowUpMethod(FollowUpMethodEntity methodEntity) {
        // 设置默认排序号
        if (methodEntity.getSortOrder() == null) {
            Integer maxSortOrder = followUpMethodMapper.selectMaxSortOrder(methodEntity.getTenantId());
            methodEntity.setSortOrder(maxSortOrder == null ? 1 : maxSortOrder + 1);
        }
        
        // 设置默认状态为启用
        if (methodEntity.getStatus() == null) {
            methodEntity.setStatus("enabled");
        }
        
        // 设置非系统内置
        if (methodEntity.getIsSystem() == null) {
            methodEntity.setIsSystem(0);
        }
        
        return this.save(methodEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateFollowUpMethod(FollowUpMethodEntity methodEntity) {
        return this.updateById(methodEntity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFollowUpMethod(Long id) {
        FollowUpMethodEntity methodEntity = this.getById(id);
        if (methodEntity != null && methodEntity.getIsSystem() == 1) {
            throw new RuntimeException("系统内置的跟进方式不能删除");
        }
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean batchDeleteFollowUpMethod(Long[] ids) {
        for (Long id : ids) {
            FollowUpMethodEntity methodEntity = this.getById(id);
            if (methodEntity != null && methodEntity.getIsSystem() == 1) {
                throw new RuntimeException("系统内置的跟进方式不能删除");
            }
        }
        return this.removeByIds(Arrays.asList(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean enableFollowUpMethod(Long id) {
        FollowUpMethodEntity methodEntity = this.getById(id);
        if (methodEntity != null) {
            methodEntity.setStatus("enabled");
            return this.updateById(methodEntity);
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean disableFollowUpMethod(Long id) {
        FollowUpMethodEntity methodEntity = this.getById(id);
        if (methodEntity != null) {
            methodEntity.setStatus("disabled");
            return this.updateById(methodEntity);
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSortOrder(Long id, Integer sortOrder) {
        FollowUpMethodEntity methodEntity = this.getById(id);
        if (methodEntity != null) {
            methodEntity.setSortOrder(sortOrder);
            return this.updateById(methodEntity);
        }
        return false;
    }
}